﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;

namespace Lenovo.CFI.Web.WS
{
    public class SuggestUser
    {
        public string value = "";
        public string display = "";
    }

    /// <summary>
    /// Summary description for AjaxUser
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    [GenerateScriptType(typeof(SuggestUser))]
    public class AjaxUser : System.Web.Services.WebService
    {
        [WebMethod(true)]
        public SuggestUser[] CheckItCodes(string codes)
        {
            List<SuggestUser> sus = new List<SuggestUser>();

            List<User> users = UserCache.GetAllUser();
            //string[] codesArr = codes.ToLower().Split(new string[] { ";", "\r", "\n", "；", "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] codesArr = CodeList.SplitStringToList(codes.ToLower());

            int i = 0; int j = 0;
            while (i < codesArr.Length && j < users.Count)
            {
                if (codesArr[i] == users[j].ItCode)
                {
                    SuggestUser su = new WS.SuggestUser();
                    su.value = users[j].ItCode;
                    su.display = users[j].ItCode;
                    sus.Add(su);

                    i++; j++;
                }
                else if (codesArr[i].CompareTo(users[j].ItCode) < 0)
                    i++;
                else
                    j++;
            }

            return sus.ToArray();
        }

        [WebMethod(true)]
        public SuggestUser[] CheckItCodesOrEmail(string codes)
        {
            List<SuggestUser> sus = new List<SuggestUser>();

            List<User> users = UserCache.GetAllUser();
            //string[] codesArr = codes.ToLower().Split(new string[] { ";", "\r", "\n", "；", "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] codesArr = CodeList.SplitStringToList(codes.ToLower());

            int i = 0; int j = 0;
            while (i < codesArr.Length && j < users.Count)
            {
                if (codesArr[i].Contains("@"))
                {
                    SuggestUser su = new WS.SuggestUser();
                    su.value = codesArr[i].Trim();
                    su.display = su.value;
                    sus.Add(su);

                    i++;
                }
                else if (codesArr[i] == users[j].ItCode)
                {
                    SuggestUser su = new WS.SuggestUser();
                    su.value = users[j].Email;
                    su.display = su.value;
                    sus.Add(su);

                    i++; j++;
                }
                else if (codesArr[i].CompareTo(users[j].ItCode) < 0)
                {
                    SuggestUser su = new WS.SuggestUser();
                    su.value = codesArr[i].Trim() + "@lenovo.com";
                    su.display = su.value;
                    sus.Add(su);

                    i++;
                }
                else
                    j++;
            }

            return sus.ToArray();
        }
    }



    public class UserCache
    {
        private static readonly string CACHE_USER_ALL = "CacheUserAll";

        public static List<User> GetAllUser()
        {
            List<User> users = new List<User>();
            if (HttpContext.Current.Cache[CACHE_USER_ALL] != null)
                users = (List<User>)HttpContext.Current.Cache[CACHE_USER_ALL];
            else
            {
                users = new UserBl().GetUserAll();
                HttpContext.Current.Cache.Insert(CACHE_USER_ALL, users, null,
                    DateTime.Now.AddSeconds(600),//System.Web.Caching.Cache.NoAbsoluteExpiration,
                    TimeSpan.Zero); //TimeSpan.FromMinutes(5));
            }

            return users;
        }
    }

}